import gtk
import gtk.glade
import os, sys

from Collection import Collection


class PathModel:
	def __init__(self, _col):
		self.tree_store = gtk.TreeStore( str )
		path_list = _col.get_path_list()
		for p in path_list:
			self.tree_store.append(None, [p])
	def get_model(self):
		if self.tree_store:
			return self.tree_store
		else:
			return None


class EditPaths:
	def __init__(self):
		self.col = Collection(_debug=True)
		wTree=gtk.glade.XML ("libmanage.glade","path_window")

		dic = {  "on_apply_button_clicked" : self.on_apply_button_clicked,
				 "on_path_window_destroy_event" : self.on_path_window_destroy_event, 
				 "on_cancel_button_clicked" : self.on_cancel_button_clicked, 
				 "on_add_button_clicked" : self.on_add_button_clicked,
				 "on_remove_button_clicked" : self.on_remove_button_clicked,
				 "on_scan_button_clicked" : self.on_scan_button_clicked
				 }
		wTree.signal_autoconnect(dic)
		
		
		self.window = wTree.get_widget('path_window')
		self.tv_window = wTree.get_widget('tv_window')
		self.entry =  wTree.get_widget('path_entry')
		
		self.treestore = PathModel(self.col).get_model()
		treeview = gtk.TreeView(self.treestore)
		tvcolumn = gtk.TreeViewColumn('Paths')
		treeview.append_column(tvcolumn)
		cell = gtk.CellRendererText()
		cell.set_property('editable', True)
		cell.connect( 'edited', self.on_edit_tree, self.treestore )
		tvcolumn.pack_start(cell, True)
		tvcolumn.add_attribute(cell, 'text', 0)
		treeview.set_search_column(0)
		tvcolumn.set_sort_column_id(0)
		treeview.set_reorderable(True)
		self.tv_window.add(treeview)
		self.window.show_all()

	def on_apply_button_clicked(self, event):
		print "clicked"

	def on_cancel_button_clicked(self, event):
		print "cancel clicked"
	
	def on_add_button_clicked(self, event):
		if self.entry.get_text():
			self.treestore.append(None, [self.entry.get_text()])
			self.col.add_path(self.entry.get_text())
	
	def on_remove_button_clicked(self, event):
		print "remove button clicked"

	def on_scan_button_clicked(self, event):
		scan = ScanLibrary()

	def on_edit_tree(self, cell, path, new_text, model ):
		self.col.edit_path(model[path][0],new_text)
		model[path][0] = new_text
	
	def on_path_window_destroy_event(self, event, data):
		gtk.main_quit()





class ScanLibrary:
	def __init__(self):
		col = Collection()
		wTree2=gtk.glade.XML ("libmanage.glade",'scan_window')
		scan_window = wTree2.get_widget('scan_window')
		self.progress = wTree2.get_widget('scan_progress')
		scan_window.show_all()
		col.scan_paths_cb(self.scanning_cb)
		self.progress.set_text("Finished!")


	def scanning_cb(self, data):
		self.progress.pulse()
		self.progress.set_text(data)
		gtk.main_iteration(block=False)		



class Player:
	def __init__(self):
			col = Collection()
			wTree=gtk.glade.XML ("player.glade",'player_window')
			player_window = wTree.get_widget('player_window')
			lib_holder = wTree.get_widget('library_tree_ali')
			playlist_holder = wTree.get_widget('playlist_notebook_ali')
			lib_list = wTree.get_widget('library_list')
			play = Playlist()
			play.add_page("* Untitled")
			playlist_holder.add(play.get_widget())
			playlist_holder.show()
			lib = Library()
			lib_list.add(lib.get_widget())
			player_window.show_all()

class Library:
	def __init__(self):
		col = Collection()
		artists = col.get_all_artists()
		self.treestore = gtk.TreeStore(str)
		for a in artists:
			self.treestore.append(None, [a.name])
		self.treeview = gtk.TreeView(self.treestore)
		self.treeview.set_headers_clickable(True)
		self.treeview.set_rules_hint(True)
		self.treeview.set_search_column(0)
		artist_col = gtk.TreeViewColumn('Artist')

		self.treeview.append_column(artist_col)

		self.cell = gtk.CellRendererText()
		artist_col.pack_start(self.cell, True)

		artist_col.add_attribute(self.cell, 'text', 0)

		artist_col.set_sort_column_id(0)

		self.treeview.set_reorderable(True)

	def get_widget(self):
		return self.treeview




class Playlist:
	def __init__(self):
		col = Collection()
		self.notebook = gtk.Notebook()
		self.p = PlaylistTree(col.get_all_songs())

	def get_widget(self):
		return self.notebook

	def add_page(self, _title):
		label = gtk.Label(_title)
		scroll = gtk.ScrolledWindow()
		scroll.add(self.p.get_widget())
		self.notebook.append_page(scroll, label)

class PlaylistTree:
	def __init__(self, _song_list):
		self.treestore = gtk.TreeStore(str, str, str, str)
		for s in _song_list:
			self.treestore.append(None, [s.artist, s.album, s.track, s.title])
		self.treeview = gtk.TreeView(self.treestore)
		self.treeview.set_headers_clickable(True)
		self.treeview.set_rules_hint(True)
		self.treeview.set_search_column(0)
		artist_col = gtk.TreeViewColumn('Artist')
		album_col = gtk.TreeViewColumn('Album')
		track_col = gtk.TreeViewColumn('Track')
		title_col = gtk.TreeViewColumn('Title')
		self.treeview.append_column(artist_col)
		self.treeview.append_column(album_col)
		self.treeview.append_column(track_col)
		self.treeview.append_column(title_col)
		self.cell = gtk.CellRendererText()
		artist_col.pack_start(self.cell, True)
		album_col.pack_start(self.cell, True)
		track_col.pack_start(self.cell, True)
		title_col.pack_start(self.cell, True)
		artist_col.add_attribute(self.cell, 'text', 0)
		album_col.add_attribute(self.cell, 'text', 1)
		track_col.add_attribute(self.cell, 'text', 2)
		title_col.add_attribute(self.cell, 'text', 3)
		artist_col.set_sort_column_id(0)
		album_col.set_sort_column_id(1)
		track_col.set_sort_column_id(2)
		title_col.set_sort_column_id(3)
		self.treeview.set_reorderable(True)

	def get_widget(self):
		return self.treeview

